package com.elmcode.server.service.impl;

import com.elmcode.server.constant.StatusConst;
import com.elmcode.server.dto.FoodDTO;
import com.elmcode.server.entity.CategoryEntity;
import com.elmcode.server.exception.ParamsException;
import com.elmcode.server.service.CategoryService;
import com.elmcode.server.service.FoodService;
import com.elmcode.server.vo.shopDetailVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.Query;

import com.elmcode.server.dao.FoodDao;
import com.elmcode.server.entity.FoodEntity;


@Service("foodService")
public class FoodServiceImpl extends ServiceImpl<FoodDao, FoodEntity> implements FoodService {

    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<FoodEntity> queryWrapper = new QueryWrapper<>();
        String name = (String) params.get("name");
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        String categoryId = (String) params.get("categoryId");
        if(!StringUtils.isEmpty(categoryId)){
            queryWrapper.eq("category_id",categoryId);
        }

        IPage<FoodEntity> page = this.page(
                new Query<FoodEntity>().getPage(params),
                queryWrapper
        );


        //TODO 通过分类id，查找到分类名
        List<FoodEntity> records = page.getRecords();

        for (FoodEntity record : records) {
            CategoryEntity category = categoryService.getById(record.getCategoryId());
            record.setCategoryName(category.getName());
        }

        return new PageUtils(page);
    }

    @Override
    public void saveFood(FoodDTO food) {
        FoodEntity foodEntity = new FoodEntity();

        BeanUtils.copyProperties(food,foodEntity);

        /**
        	private String favor;
            private Date updateTime;
         */

        foodEntity.setFavor(food.getFlavor());
        foodEntity.setUpdateTime(new Date());
        foodEntity.setCreateTime(new Date());
        foodEntity.setStatus(StatusConst.ACTIVE_STATUS);

        this.baseMapper.insert(foodEntity);

    }

    @Override
    public void updateStatus(Long id, String status) throws ParamsException {
        if(status == null){
            throw new ParamsException("参数错误");
        }
        Integer updateStatus = -1;
        if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            updateStatus = StatusConst.ACTIVE_STATUS;
        }
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            updateStatus = StatusConst.INACTIVE_STATUS;
        }

        if(updateStatus != -1){
            FoodEntity foodEntity = new FoodEntity();
            foodEntity.setId(id);
            foodEntity.setStatus(updateStatus);
            foodEntity.setUpdateTime(new Date());
            this.baseMapper.updateById(foodEntity);
        }else {
            throw new ParamsException("参数错误");
        }
    }

    @Override
    public List<shopDetailVo> selectByCategoryId(Long categoryId) {
        QueryWrapper<FoodEntity> wrapper = new QueryWrapper<FoodEntity>().eq("category_id", categoryId);
        List<shopDetailVo> collect = this.baseMapper.selectList(wrapper).stream().map(item -> {
            shopDetailVo detailVo = new shopDetailVo();

            detailVo.setFlavor(item.getFavor());
            BeanUtils.copyProperties(item, detailVo);

            return detailVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void updateFoodInfo(Long id, FoodDTO food) {

        FoodEntity foodEntity = new FoodEntity();
        BeanUtils.copyProperties(food,foodEntity);

        foodEntity.setId(id);
        foodEntity.setUpdateTime(new Date());
        foodEntity.setFavor(food.getFlavor());
        foodEntity.setStatus(StatusConst.ACTIVE_STATUS);

        this.baseMapper.updateById(foodEntity);

    }

}